Passed
Push — master ( b15b71...ab24e8 )
by Paul
04:46
created

Recaptcha.onKeyup_   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 9.4285
1
/** global: GLSR, grecaptcha */
2
;(function() {
3
4
	'use strict';
5
6
	var Recaptcha = function( form ) { // Form object
7
		this.Form = form;
8
		this.addListeners = this.addListeners_;
9
		this.execute = this.execute_;
10
		this.reset = this.reset_;
11
	};
12
13
	Recaptcha.prototype = {
14
15
		/** @return void */
16
		addListeners_: function() {
17
			var overlayEl = this.getOverlay_();
18
			if( overlayEl === -1 )return;
19
			overlayEl.addEventListener( 'click', this.Form.enableButton );
20
			window.addEventListener( 'keyup', this.onKeyup_.bind( this, overlayEl ));
21
		},
22
23
		/** @return void */
24
		execute_: function() {
25
			var recaptchaId = this.getId_();
26
			if( recaptchaId !== -1 ) {
27
				grecaptcha.execute( recaptchaId );
28
				return;
29
			}
30
			// recaptcha ID not found so pass through an error
31
			this.Form.submitForm_( false );
32
		},
33
34
		/** @return string|int (-1) */
35
		getId_: function() {
36
			return this.search_( function( value, id ) {
37
				if( Object.prototype.toString.call( value ) !== '[object HTMLDivElement]' )return;
38
				if( value.closest( 'form' ) === this.Form.form ) {
1 ignored issue
show
Complexity Best Practice introduced by
There is no return statement if value.closest("form") === this.Form.form is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
39
					return id;
40
				}
41
			});
42
		},
43
44
		/** @return HTMLDivElement|int (-1) */
45
		getOverlay_: function() {
46
			return this.search_( function( value ) {
47
				if( Object.prototype.toString.call( value ) !== '[object Object]' )return;
48
				for( var obj in value) {
49
					if( !value.hasOwnProperty( obj ) || Object.prototype.toString.call( value[obj] ) !== '[object HTMLDivElement]' )continue;
50
					if( value[obj].className === '' ) {
51
						return value[obj].firstChild;
52
					}
53
				}
54
				return false;
55
			});
56
		},
57
58
		/** @return void */
59
		onKeyup_: function( ev ) { // KeyboardEvent
60
			if( ev.keyCode !== 27 )return;
61
			this.Form.enableButton();
62
			this.removeListeners_( ev.target );
63
		},
64
65
		/** @return void */
66
		removeListeners_: function( overlayEl ) { // HTMLDivElement
67
			overlayEl.removeEventListener( 'click', this.Form.enableButton );
68
			window.removeEventListener( 'keyup', this.onKeyup_ );
69
		},
70
71
		/** @return void */
72
		reset_: function() {
73
			var recaptchaId = this.getId_();
74
			if( recaptchaId !== -1 ) {
75
				grecaptcha.reset( recaptchaId );
76
			}
77
		},
78
79
		/** @return mixed|int (-1) */
80
		search_: function( callback ) { // function
81
			var result = -1;
82
			if( window.hasOwnProperty( '___grecaptcha_cfg' )) {
83
				var clients = window.___grecaptcha_cfg.clients;
84
				var i, key;
85
				for( i in clients ) {
86
					for( key in clients[i] ) {
87
						if( !( result = callback( clients[i][key], i ).bind( this )))continue;
88
						return result;
89
					}
90
				}
91
			}
92
			return result;
93
		},
94
	};
95
96
	GLSR.Recaptcha = Recaptcha;
97
})();
98